package com.itheima.web.controller;


import com.itheima.domain.system.Module;
import com.itheima.domain.system.User;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String email, String password) {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            request.setAttribute("error", "用户名或密码不能为空！");
            return "forward:login.jsp";
        }

        try {
            UsernamePasswordToken token = new UsernamePasswordToken(email, password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);

            User user = (User) subject.getPrincipal();
            session.setAttribute("loginUser", user);
            List<Module> moduleList = moduleService.findByUser(user);
            session.setAttribute("modules", moduleList);

            return "/home/main";
        } catch (AuthenticationException e) {
            request.setAttribute("error", "邮箱或密码错误！");
            return "forward:login.jsp";
        }

        /*User user = userService.findByEmail(email);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                session.setAttribute("loginUser", user);
                List<Module> moduleList = moduleService.findByUser(user);
                session.setAttribute("modules",moduleList);
                return "/home/main";
            } else {
                request.setAttribute("error", "密码错误！");
                return "forward:login.jsp";
            }
        } else {
            request.setAttribute("error", "用户名不存在！");
            return "forward:login.jsp";
        }*/
    }

    //退出
    @RequestMapping(value = "/logout", name = "用户登出")
    public String logout() {
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home() {
        return "home/home";
    }

    @RequestMapping("/changePassword")
    public @ResponseBody
    int changePassword(String oldPassword, String newPassword) {
        User user = loginUser;
        oldPassword = new Md5Hash(oldPassword,user.getEmail(),1).toString();
        if (oldPassword.equals(user.getPassword())) {
            //1代表密码正确,然后更新密码
            user.setPassword(newPassword);
            userService.update(user);
            return 1;
        } else {
            return 0;
        }
    }
}
